home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $readloop.P < prev    next >
Text File  |  1990-04-12  |  14KB  |  421 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $readloop.P */
  25.  
  26. /* This is the top-level read-eval-print loop for Prolog. 
  27.  
  28.    $readloop also sets up some global values to allow it to handle
  29.    break levels. It defines break/0 and abort/0. (repeat/0 is also
  30.    defined here, for lack of a better place for now.)
  31.  
  32. $readl_export([break/0,abort/0,repeat/0]).
  33.  */
  34.  
  35. $readloop :- 
  36.     $init_sys,
  37.     $load_mod($io),
  38.     $load_mod($read),
  39.     $load($prorc),call($prorc),
  40.     $globalset('_$abort_cutpoint'(0)),
  41.     $globalset('_$break_level'(0)),
  42.     $writename('SB-Prolog Version '), $version(V), $writename(V), $nl,
  43.     $readlp1.
  44.  
  45. '_$tab_size'(17).
  46.  
  47. $readlp1 :- repeat,'_$savecp'(Cp),
  48.     $globalset_a('_$abort_cutpoint'(Cp),Cp),
  49.     $flags(2,0), /* set flag to intercept stack overflow */
  50.     $globalset('_$break_level'(0)),
  51.     $readl_userio(I,O),
  52.     $writename('| ?- '),
  53.     $read(X, Vars),
  54.     $expand_body(X,Y),
  55.     $readl_resetio(I,O),
  56.     ($readl_stop(Y),!;$readl_procq(Y,Vars)).
  57.  
  58. $readl_stop(halt) :- halt. /* quit entire system, regardless of break level */
  59. $readl_stop(end_of_file).    /* pop a break level, out if at top */
  60.  
  61. $readl_procq(X,[]) :- !,$readl_docall(X),!,
  62.     $readl_userio(I,O),$writename(yes),$nl,$readl_resetio(I,O),fail.
  63.  
  64. $readl_procq(X,Vars) :- $readl_docall(X),
  65.     $readl_userio(I,O),$readl_printans(Vars),
  66.     $readl_ifnomo(I,O),!,$writename(yes),$nl,$readl_resetio(I,O),fail.
  67.  
  68. $readl_ifnomo(_,_) :- $get0(10),!.
  69. $readl_ifnomo(I,O) :- $readl_ifnomo(I,O),$readl_resetio(I,O),fail.
  70.  
  71. $readl_docall(X) :- call(X).
  72. $readl_docall(_) :- $readl_userio(I,O),$writename(no),$nl,
  73.     $readl_resetio(I,O),fail.
  74.  
  75. $readl_printans([]) :- !.
  76. $readl_printans([=(Name, Val)|Tail]) :-
  77.         $nl,$writename(Name),
  78.         $writename(' = '),
  79.         $write(Val),
  80.         $readl_printans(Tail).
  81.  
  82. break :- '_$break_level'(Blevel),
  83.     Nblevel is Blevel+1, $globalset('_$break_level'(Nblevel)),
  84.     $readl_userio(I,O),
  85.     $writename('[ Break (level '),$writename(Nblevel),
  86.     $writename(') ]'),$nl,
  87.     $readl_brklp1,
  88.     $globalset('_$break_level'(Blevel)),
  89.     $writename('[ End break (level '),$writename(Nblevel),
  90.     $writename(') ]'),$nl,
  91.     $readl_resetio(I,O).    /* should we reset here ? */
  92.  
  93. $readl_brklp1 :- repeat,'_$break_level'(Blevel),
  94.     $writename(Blevel),$writename(': ?- '),
  95.     $read(X, Vars),
  96.     ($readl_stop(X),!;$readl_procq(X,Vars)).
  97.  
  98. abort :- '_$abort_cutpoint'(Cp), '_$cutto'(Cp), fail.
  99.  
  100. $readl_userio(Oi,Oo) :- 
  101.     $seeing(Oi),$see(user),
  102.     $telling(Oo),$tell(user).
  103.  
  104. $readl_resetio(Oi,Oo) :- $see(Oi),$tell(Oo).
  105.  
  106. repeat.
  107. repeat :- repeat.
  108.  
  109. $flags(Which,What) :- '_$builtin'(113).
  110.  
  111.  
  112. /* These are the module handling routines. The main routine is the
  113.    undefined_pred interrupt handler. The system keeps a table of modules
  114.    and routines that are needed from each. When a predicate is found to
  115.    be undefined, the table is searched to see if it is defined by 
  116.    some module. If so, that module is loaded (if it hasn't been previously
  117.    loaded) and the association is made between the routine name as 
  118.    defined in the module, and the routine name as used by the invoker.
  119.  
  120.    The table of modules and needed routines is:
  121.    defined_mods(modname,[pred1/arity1,...,predn/arityn]).
  122.      where modname is the name of the module. The module exports n
  123.     predicate definitions. The first exported pred is of arity 
  124.     arity1, and needs to be invoked by the name of pred1.
  125.  
  126.    The table of modules that have already been loaded is:
  127.    loaded_mods(modname).
  128.  
  129.    A module is a file of predicate definitions. Consider a module name
  130.    `mod'. It contains a single fact, named mod_export, that is true of
  131.    the list of predicate/arity's that are exported. 
  132.    e.g. mod_export([mod_p1/2,mod_p2/4]).
  133.    For each module, m, which contains predicates needed by this module,
  134.    there is a mod_use fact, describing what module is needed and the names
  135.    of the predicates defined there that are needed. For example, if module
  136.    mod needs to import predicates from mod m, there would be a fact:
  137.    mod_use(m,[mod_ip1/2,mod_ip2/4]), where m is a module that exports two
  138.    predicates: one 2-ary and one 4-ary. This list corresponds to the export
  139.    list of module m.
  140.  
  141. */
  142. $init_sys_export([$define_mod/2,$load/1,$load_mod/1]).
  143.  
  144. $init_sys :- 
  145.     $load($opcode),    /* THIS FILE MUST BE THE FIRST LOADED */
  146.     $load($assert),
  147.     $load($db),
  148.     $load($dbcmpl),
  149.     $load($bio),
  150.     $load($bmeta),
  151.     $load($name),
  152.     $load($blist),
  153.     $load($call),
  154.     $load($glob),
  155.     $load($funrel),
  156.     $assert_union(loaded_mods(_),$loaded_mods(_)), /* nec for compile */
  157.     $assert_abolish_i(defined_mods(_,_)),    /* nec for compile [a] */
  158.     $globalset('_$nofile_msg'(1)).
  159.  
  160. $loaded_mods($buff). /* with readloop */
  161. $loaded_mods($init_sys). /* with readloop */
  162. $loaded_mods($assert).
  163. $loaded_mods($db).
  164. $loaded_mods($dbcmpl).
  165. $loaded_mods($bio).
  166. $loaded_mods($bmeta).
  167. $loaded_mods($name).
  168. $loaded_mods($blist).
  169. $loaded_mods($call).
  170. $loaded_mods($glob).
  171. $loaded_mods($funrel).
  172.  
  173.  
  174. $define_mod(Mod,Implist) :- defined_mods(Mod,Implist),!. /* no-op if there */
  175. $define_mod(Mod,Implist) :- $assert(defined_mods(Mod,Implist)).
  176.  
  177. /* the undefined_pred interrupt handler: */
  178.  
  179. '_$undefined_pred'(Term) :-
  180.     $functor0(Term,Pred),
  181.     $arity(Term,Arity),
  182.     ('_$nodynload'(Pred,Arity) ->
  183.         fail ;
  184.         (not(not('_$definable'(Term))),'_$call'(Term))
  185.     ).
  186.  
  187. '_$nodynload'(_,_) :- fail.
  188.  
  189. '_$definable'(Term) :- 
  190.     $functor0(Term,Pred),$arity(Term,Arity),
  191. /*      $telling(F),$tell(user),$writename('loading: '),$writename(Pred),
  192.       $writename('/'),$writename(Arity),$nl,$tell(F), */
  193.     defined_mods(Module_name,Pred_name_list),  /* for each module.. */
  194.     $init_membernv(Pred/Arity,Pred_name_list), 
  195.         /* is needed pred in this one? */
  196.     !,                /* yes! */
  197.     $load_mod(Module_name),        /* go load it if necessary */
  198.     $name(Module_name,Mod_name_chars),
  199.     $append(Mod_name_chars,"_export",Exp_name_chars),
  200.     $name(Exp_name,Exp_name_chars),
  201.     $bldstr(Exp_name,1,Modcall),arg(1,Modcall,Explist),
  202.     '_$call'(Modcall),
  203.     $load_preds(Explist,Pred_name_list,Module_name). 
  204.  
  205. '_$definable'(Term) :-    /* no module to define pred */
  206.     $functor0(Term,Pred),
  207.     $load(Pred),!,        /* file exists */
  208.     (not($pred_undefined(Term))    /* file defines pred */
  209.      ;
  210.      $arity(Term,Arity),
  211.      $telling(Fi),$tell(user),
  212.      $writename(Pred),$writename('/'),$writename(Arity),
  213.      $writename(' not defined by file'),$nl,$tell(Fi),fail
  214.     ).
  215.  
  216. '_$definable'(Term) :- 
  217.     '_$nofile_msg'(1),    /* if message is to be displayed */
  218.     $telling(Fi),$tell(user),
  219.     $writename('No file to define '),
  220.     $functor0(Term,Pred),$arity(Term,Arity),
  221.     $writename(Pred),$writename('/'),$writename(Arity),$nl,$tell(Fi),fail.
  222.  
  223. $load_mod(Module_name) :-
  224.     loaded_mods(Module_name),!.    /* already loaded, noop */
  225. $load_mod(Module_name) :-
  226. /*       $telling(Fi),$tell(user),$writename('load module: '),
  227.       $writename(Module_name),$nl,$tell(Fi), */
  228.     $load(Module_name),!,         /* now loaded */
  229.     $assert(loaded_mods(Module_name)).
  230.     /*    $load_sub_mods(Module_name). */
  231. $load_mod(Module_name) :-        /* no such file */
  232.     $telling(Fi),$tell(user),
  233.     $writename('No file to define module '),
  234.     $writename(Module_name),$nl,$tell(Fi),fail.
  235.  
  236. /* $load_sub_mods(Module_name) :-
  237.     $name(Module_name,Mod_name_chars),
  238.     $append(Mod_name_chars,"_use",Use_name_chars),
  239.     $name(Use_name,Use_name_chars),
  240.     $bldstr(Use_name,2,Modusecall),
  241.     not($pred_undefined(Modusecall)),
  242.     arg(1,Modusecall,M),arg(2,Modusecall,U),
  243.     '_$call'(Modusecall),not(defined_mods(M,U)),!,
  244.     $assert_union(defined_mods(_,_),Modusecall).
  245.  
  246. $load_sub_mods(_). */
  247.  
  248. $load_preds([],[],_) :- !.
  249. $load_preds([],[_|_],Mod) :- !,
  250.     $telling(Fi),$tell(user),
  251.     $writename('Illegal use list for module: '),
  252.     $writename(Mod),$nl,$tell(Fi),fail.
  253. $load_preds([_|_],[],Mod) :- !,
  254.     $telling(Fi),$tell(user),
  255.     $writename('Illegal use list for module: '),
  256.     $writename(Mod),$nl,$tell(Fi),fail.
  257. $load_preds([Inname|Explist],[Inname|Pred_name_list],Mod) :- !,
  258.     $load_preds(Explist,Pred_name_list,Mod).
  259. $load_preds([Inname/Arity|Explist],[Outname/Arity|Pred_name_list],
  260.         Mod) :- !,
  261.     $bldstr(Outname,Arity,Outstr),$bldstr(Inname,Arity,Instr),
  262.     ($pred_undefined(Outstr),!,$assert_union(Outstr,Instr),
  263.         $load_preds(Explist,Pred_name_list,Mod)
  264.     ;
  265.         $telling(Fi),$tell(user),
  266.         $writename('Attempt to redefine '),$writename(Outname),
  267.         $writename('/'),$writename(Arity),$nl,
  268.         $tell(Fi),
  269.         $load_preds(Explist,Pred_name_list,Mod)
  270.     ).
  271. $load_preds([Inname/Inarity|Explist],[Outname/Outarity|Pred_name_list],
  272.         Mod) :-
  273.     $telling(Fi),$tell(user),
  274.     $writename('Incorrect import arity: '),$writename(Outname),
  275.     $writename('/'),$writename(Outarity),$nl,
  276.     $tell(Fi),
  277.     $load_preds(Explist,Pred_name_list,Mod),
  278.     fail.
  279.  
  280.  
  281. $init_membernv(Pa,[Tpa|_]) :- nonvar(Tpa),Pa=Tpa.
  282. $init_membernv(Pa,Pn_list) :- nonvar(Pn_list),
  283.     Pn_list=[_|Pn_tail],$init_membernv(Pa,Pn_tail).
  284.  
  285.  
  286. /* the keyboard interrupt handler */
  287.  
  288. '_$keyboard_int'(Call) :- break,'_$call'(Call).
  289.  
  290. /* the general interrupt handler! */
  291.  
  292. '_$interrupt'(Call,Code) :-
  293.     Code =:= 0 -> '_$undefined_pred'(Call);
  294.       (Code =:= 1 -> '_$keyboard_int'(Call);
  295.           (Code =:= 2 -> 
  296.         $tell(user),$writename('Stack Overflow!!'),$nl,abort
  297.         ;
  298.         $writename('Illegal interrupt code'),halt
  299.           )
  300.       ).
  301.  
  302. /********************************************************/
  303. /* This is the dynamic load routine. It gets the SIMPATH global variable
  304. and uses it to determine what files to try to load. */
  305.  
  306. load(File) :- $load(File).    /* for now */
  307. $load(File) :- $buff_code(File, 0, 31 /*gb*/,0'/) -> $loadqual(File,0);
  308.             not(not($nnload(File))).
  309.  
  310. $loadqual(File,Rc) :- '_$builtin'(11).
  311.  
  312. $nnload(File) :- 
  313.     $conlength(File,Flen),
  314.     $readl_simpath(Dir),
  315.     $conlength(Dir,Dlen),Wlen is Flen+Dlen+1,
  316.     $alloc_heap(Wlen,Wname),
  317.     $substring(0,Dlen,Dir,0,Wname,Dlen),
  318.     $substring(0,1,'/',Dlen,Wname,Floc),
  319.     $substring(0,Flen,File,Floc,Wname,Wlen),
  320.     $loadqual(Wname,0),! .
  321.  
  322. $readl_simpath(Dir) :- 
  323.     $getenv_simpath(Simpath),
  324.     $readl_getenvdir(Simpath,0,Dir).
  325.  
  326. $readl_getenvdir(Simpath,Loc,Dir) :- 
  327.     $subdelim(1,':',Dir1,Loc,Simpath,Nloc) ->
  328.         (Dir = Dir1;
  329.          $readl_getenvdir(Simpath,Nloc,Dir)
  330.         )
  331.     /* else */ ;
  332.         $conlength(Simpath,Len),Llen is Len-Loc,
  333.         $substring(1,Llen,Dir,Loc,Simpath,_).
  334.  
  335. /* get environment variable value */
  336.  
  337. $getenv_simpath(X) :- '_$builtin'(12).
  338.  
  339. /* $buff *******************************************************/
  340. /* needed for dynamic loader */
  341.  
  342. $buff_export([$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,$symtype/2,
  343.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  344.         $pred_undefined/1, $hashval/3]).
  345.  
  346. $alloc_perm(Size, Buff) :- $alloc_buff(Size,Buff,0,0,0).
  347.  
  348. $alloc_heap(Size, Buff) :- $alloc_buff(Size,Buff,1,0,0).
  349.  
  350. /* Type 0: perm, 1: heap, 2: from Supbuff */
  351. $alloc_buff(Size,Buff,Type,Supbuff,Retcode) :- 
  352.     $alloc_buff1(Size,Buff,Type,Supbuff,Retcode),
  353.     (Retcode =\= 0 -> 
  354.         $writename('alloc failed'),$nl,fail;
  355.         true).
  356. $alloc_buff1(Size,Buff,Type,Supbuff,Retcode) :- '_$builtin'(76).
  357.  
  358. $trimbuff(Size,Buff,Type) :- '_$builtin'(79).
  359. $trimbuff(Size,Buff,Type,Supbuff) :- '_$builtin'(79).
  360.  
  361. $buff_code(Buff,Offset,Disc,Term) :- '_$builtin'(77).
  362.  
  363. /* Type = 0: no ep, 1: dynamic, 2: ep to compiled code, 3: buffer */
  364. $symtype(Term,Type) :- '_$builtin'(42).
  365.  
  366. $substring(Dir,NumBytes,Const,Locin,Buff,Locout) :- '_$builtin'(51).
  367.  
  368. $subnumber(Dir,NumBytes,NumCon,Locin,Buff,Locout) :- '_$builtin'(52).
  369.  
  370. $subdelim(Dir,Delim,Const,Locin,Buff,Locout) :- '_$builtin'(53).
  371.  
  372. $conlength(Const,Len) :- '_$builtin'(54).
  373.  
  374. $pred_undefined(Term) :- 
  375.     $symtype(Term,D),
  376.     (D=:=0;
  377.      D=\=0,D=:=3).
  378.  
  379. $hashval(Arg, Size, Hashval) :- '_$builtin'(43).
  380.  
  381. /* These routines put numbers into buffers in internet format */
  382. $buff_putnum_n(Buff,Loc,Len,Num) :-
  383.     Len =< 1 -> 
  384.         $buff_code(Buff,Loc,30 /*pb*/ ,Num)
  385.     /* else */ ;
  386.         Byte is Num /\ 255,
  387.         Rest is Num >> 8,
  388.         Nlen is Len-1,Sloc is Loc+Nlen,
  389.         $buff_code(Buff,Sloc,30,Byte),
  390.         $buff_putnum_n(Buff,Loc,Nlen,Rest).
  391.  
  392. $buff_getnum_n(Buff,Loc,Len,Num) :-
  393.     Len =< 1 -> 
  394.         $buff_code(Buff,Loc,31 /*gb*/ ,Num)
  395.     /*else*/ ;
  396.         Nlen is Len-1,Sloc is Loc+Nlen,
  397.         $buff_code(Buff,Sloc,31,Byte),
  398.         $buff_getnum_n(Buff,Loc,Nlen,Rest),
  399.         Num is (Rest << 8) + Byte.
  400.  
  401. $globalset_a(Place,Value) :- 
  402.     integer(Value) -> 
  403.          ($opcode( getnumcon, GetNumOp ),
  404.               $buff_code(Place, 0, 7, /*gepb*/ Buff),
  405.           $buff_code(Buff,  4, 3, /*ps*/   GetNumOp /*getnumcon*/),
  406.           $buff_code(Buff,  8, 2, /*pn*/   Value)
  407.          ) ;
  408.     (real(Value) ->
  409.          ($opcode( getfloatcon, GetFltOp ),
  410.               $buff_code(Place, 0, 7, /*gepb*/ Buff),
  411.           $buff_code(Buff,  4, 3, /*ps*/   GetFltOp /*getfloatcon*/),
  412.           $buff_code(Buff,  8,27, /*pf*/   Value)
  413.          )
  414.     ).
  415.  
  416. /* for debugging */
  417. $writename(X) :- '_$builtin'(133).
  418. $nl :- '_$builtin'(26).
  419.  
  420.  
  421.